package xcmg.device.service.safetyStock;

import java.math.BigDecimal;
import java.util.List;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xcmg.device.dao.entity.PriMaterialsDO;
import xcmg.device.dao.entity.basic.MaterialsPurchaseInfo;
import xcmg.device.dao.entity.company.CompanyInfoDO;
import xcmg.device.dao.entity.safetyStock.MaterialsSafetyStock;
import xcmg.device.dao.entity.safetyStock.MaterialsSalesData;
import xcmg.device.dao.mapper.CompanyInfoMapper;
import xcmg.device.dao.mapper.PriMaterialsMapper;
import xcmg.device.dao.mapper.basic.MaterialsPurchaseInfoMapper;
import xcmg.device.dao.mapper.safetyStock.MaterialsSafetyStockMapper;
import xcmg.device.service.purchase.MaterialsPurchaseInfoService;
import xcmg.device.util.DecimalUtil;
import yb.ecp.fast.infra.infra.log.LogHelper;

/**
 * @author wangyong
 */
@Service
public class SafetyInventoryCommonService {

  @Autowired
  private PriMaterialsMapper priMaterialsMapper;

  /**
   * 获取零件采购相关信息中的“交货期”字段，用于安全库存的计算，如果一个零件有多个采购信息，则取交货期最小的值来计算
   *
   * @param orgId
   * @param materialsNo
   * @return
   * @throws Exception
   */
  @Transactional(rollbackFor = Exception.class)
  protected BigDecimal getDeliverySpend(String orgId, String materialsNo) throws Exception {
    PriMaterialsDO materialsDO = priMaterialsMapper.itemByNo(materialsNo, orgId);
    if (null == materialsDO || null == materialsDO.getDeliverySpend()) {
      return null;
    }
    return materialsDO.getDeliverySpend();
  }


  /**
   * 最近一个月出货量与此之前12个月出货量平均值的数组
   *
   * @param materialsSalesData
   * @return
   */
  protected BigDecimal[] getLastMonthArray(MaterialsSalesData materialsSalesData) {
    BigDecimal[] salesArray = {
        materialsSalesData.getLastMonth2(),
        materialsSalesData.getLastMonth3(),
        materialsSalesData.getLastMonth4(),
        materialsSalesData.getLastMonth5(),
        materialsSalesData.getLastMonth6(),
        materialsSalesData.getLastMonth7(),
        materialsSalesData.getLastMonth8(),
        materialsSalesData.getLastMonth9(),
        materialsSalesData.getLastMonth10(),
        materialsSalesData.getLastMonth11(),
        materialsSalesData.getLastMonth12(),
        materialsSalesData.getLastMonth13()};
    // 最近一个月出货量与此之前12个月出货量平均值的数组
    BigDecimal[] needArray = {materialsSalesData.getLastMonth(), DecimalUtil.calAverage(salesArray,2)};
    return needArray;
  }
}
